/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Frame_Read.java
 *
 * Created on 3/07/2011, 07:26:36 PM
 */
package prueba_conexion;

import conexion.NewHibernateUtil;
import conexion.JmEmpleado;
import java.util.List;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author JoseManuel
 */
public class Frame_Read extends javax.swing.JFrame {

    /** Creates new form Frame_Read */
    public Frame_Read() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        L_con_nomemp = new javax.swing.JLabel();
        L_con_apemp = new javax.swing.JLabel();
        Con_NomEmp = new javax.swing.JTextField();
        Con_ApEmp = new javax.swing.JTextField();
        B_mostrar_cons = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        L_con_nomemp.setText("Nombre");

        L_con_apemp.setText("Apellido");

        B_mostrar_cons.setText("Mostrar Consulta");
        B_mostrar_cons.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                B_mostrar_consActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setText("Si se deja en blanco Nombre y Apellido se obtendran todos los registros");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 487, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(L_con_nomemp)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Con_NomEmp, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(L_con_apemp)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(Con_ApEmp, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(99, 99, 99)
                                .addComponent(B_mostrar_cons))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(L_con_nomemp)
                    .addComponent(Con_NomEmp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(L_con_apemp)
                    .addComponent(Con_ApEmp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(B_mostrar_cons)
                .addGap(26, 26, 26)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void B_mostrar_consActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_mostrar_consActionPerformed
        // TODO add your handling code here:
        //
       if(!Con_NomEmp.getText().trim().equals("")) {
        runQueryBasedOnFirstName();
    } else if(Con_ApEmp.getText().trim().equals("")) {
        runQueryBasedOnLastName();
    }
    }//GEN-LAST:event_B_mostrar_consActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Frame_Read().setVisible(true);
            }
        });
    }
    
    private static String QUERY_BASED_ON_FIRST_NAME="from JmEmpleado a where a.nombreEmpleado like '";
    private static String QUERY_BASED_ON_LAST_NAME="from JmEmpleado"/*"from JmEmpleado a where a.apellidoEmpleado like '"*/;

private void runQueryBasedOnFirstName() {
    executeHQLQuery(QUERY_BASED_ON_FIRST_NAME +  Con_NomEmp.getText() + "%'");
}
    
private void runQueryBasedOnLastName() {
    executeHQLQuery(QUERY_BASED_ON_LAST_NAME /*+ Con_ApEmp.getText() + "%'"*/);
}
    
private void executeHQLQuery(String hql) {
    try {
        Session session = NewHibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Query q = session.createQuery(hql);
        List resultList = q.list();
        displayResult(resultList);
        session.getTransaction().commit();
    } catch (HibernateException he) {
        he.printStackTrace();
    }
}

private void displayResult(List resultList) {
    Vector tableData = new Vector();
    Vector<String> tableHeaders = new Vector<String>();
    
    tableHeaders.add("IdEmpleado"); 
    tableHeaders.add("NombreEmpleado");
    tableHeaders.add("ApellidoEmpleado");
    tableHeaders.add("DireccionEmpleado");
    
     Vector<Object> oneRow = null;

    for(Object o : resultList) {
        JmEmpleado empleado = (JmEmpleado)o;
        oneRow = new Vector<Object>();
        //Vector<Object> oneRow = new Vector<Object>();
        oneRow.add(empleado.getIdEmpleado());
        oneRow.add(empleado.getNombreEmpleado());
        oneRow.add(empleado.getApellidoEmpleado());
        oneRow.add(empleado.getDireccionEmpleado());
        tableData.add(oneRow);
    }
    jTable1.setModel(new DefaultTableModel(tableData, tableHeaders));
    //DefaultTableModel model = new DefaultTableModel();
    //jTable1.setModel(model);
    //model.setColumnIdentifiers(tableHeaders.toArray());
    //model.addRow(tableData.toArray());
}
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton B_mostrar_cons;
    private javax.swing.JTextField Con_ApEmp;
    private javax.swing.JTextField Con_NomEmp;
    private javax.swing.JLabel L_con_apemp;
    private javax.swing.JLabel L_con_nomemp;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
